import { Node } from "cc";
import { Fish } from "./Fish";

export class FishSchool {
    fishes: Fish[];
    bestFish: Fish;
    constructor(list:any[], positionRange: number[][], velocityRange: number[][]) {
        this.fishes = [];
        for (let i = 0; i < list.length; i++) {
            let position = [];
            let velocity = [];
            for (let j = 0; j < positionRange.length; j++) {
                position.push(Math.random() * (positionRange[j][1] - positionRange[j][0]) + positionRange[j][0]);
                velocity.push(Math.random() * (velocityRange[j][1] - velocityRange[j][0]) + velocityRange[j][0]);
            }
            list[i].node.name = `fish${i}`
            let fish = new Fish(position, velocity,list[i]);
            this.fishes.push(fish);
        }
        this.bestFish = this.fishes[0];
    }

    update(func: Function) {
        for (let i = 0; i < this.fishes.length; i++) {
            this.fishes[i].updateVelocity(this.bestFish);
            this.fishes[i].updatePosition();
            this.fishes[i].calculateFitness(func);
            if (this.fishes[i].fitness < this.bestFish.fitness) {
                this.bestFish = this.fishes[i];
            }
        }
    }
}